<template>
	<view>
		<u-navbar :title="$t('确认订单')"></u-navbar>
		<view class="wrap">
			<view class="address" @click="$.to('/pagesA/pages/my/address?state=1')">
				<view class="left">
					<image src="../../static/image/a1_.png" mode=""></image>
					<view v-if="!isAdd">{{$t('添加收货地址')}}</view>
					<view class="info" v-else>
						<view class="name">
							{{ address.fullname }} <text>{{ address.mobile }}</text>
						</view>
						<view class="add">
							{{ address.province + ' ' + address.city + ' ' + address.area + ' ' + address.detailed }}
						</view>
					</view>
				</view>
				<u-icon name="arrow-right" color="#ccc"></u-icon>
			</view>

			<view class="goodsBox">
				<view class="goods" v-for="(item, idx) in info" :key="idx">
					<image :src="item.image" mode=""></image>
					<view class="goodsInfo">
						<view class="tit">{{ item.name }}</view>

						<view class="price n-flex-row n-justify-between" style="margin-top: 10rpx;">
							<view>{{$t('¥')}}0</view>
							<view style="color: #999;font-size: 28rpx;">x1</view>
						</view>
						<view style="color: #999;font-size: 28rpx;" v-if="item.goods_type == 2">{{$t('自动发货')}}</view>
					</view>
				</view>
				<view class="row">
					<view>{{$t('订单类型')}}</view>
					<view>{{$t('提货订单')}}</view>
				</view>

				<view class="row">
					<view>{{$t('运费')}}</view>
					<view class="pri" v-if="yunfei == 0">{{$t('免运费')}}</view>
					<view class="pri" v-else>{{$t('¥')}}{{ !yunfei ? yunfei : amount ? amount * njian : allFee }}</view>
				</view>
				<view class="beizhu">
					<view class="">{{$t('备注信息')}}</view>
					<u-input v-model="remarks" type="textarea" height="50" :placeholder="$t('选填备注信息')" />
				</view>
			</view>

			<view class="box1" style="margin-top: 20rpx;" v-if="yunfei > 0">
				<u-radio-group style="display: block;" width="34rpx" v-model="paymode" active-color="#70DDE0">
			
					<!-- #ifdef APP-PLUS || H5 -->
					<view class="rowitem payrow" @click="selectPay('usdt')">
						<view class="l">
							<image src="../../static/image/usdt.png" mode=""></image>
							{{$t('USDT')}}
						</view>
						<u-radio name="usdt"></u-radio>
					</view>
					<!-- #endif -->
				</u-radio-group>
			</view>
		</view>
		<view class="rule">
			<rich-text :nodes="rule"></rich-text>
		</view>
		<view style="height: 200rpx;">

		</view>

		<view class="btm">
			<view class="btmbox">
				<view class="n-flex-row n-align-center">
					<view class="">
						<view>{{$t('共')}}{{ njian }}{{$t('件')}} | {{$t('合计')}}：{{$t('¥')}}<text>{{ !yunfei ? yunfei : amount ? amount * njian : allFee }}</text>
						</view>
						<view class="yun" v-if="yunfei != 0">({{$t('含运费')}}{{$t('¥')}}{{ !yunfei ? yunfei : amount ? amount * njian : allFee
							}})</view>
						<!-- <view class="yun" v-if="yunfei == 0">单次提货满3件及以上免费包邮</view> -->
					</view>

					<button @click="pay">{{$t('提交订单')}}</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			remarks: '',
			address: '',
			isAdd: false, //是否有地址
			info: [],
			yunfei: 0,
			njian: 0,
			ids: '',
			// #ifdef MP-WEIXIN || H5
			paymode: 'usdt',
			// #endif

			// #ifdef APP-PLUS
			paymode: 'usdt',
			// #endif
			rule: '',
			amount: '',
			allFee: 0, // 商品总运费
			app_wxpay_status: null, //代表app微信支付开启，接口返回的
			app_alipay_status: null, //代表app支付宝支付开启
			epay_alipay_status: null, //代表h5支付宝开启，
			epay_wxpay_status: null, //代表h5微信支付开启
		}
	},
	onLoad(option) {
		this.ids = option.ids
		this.info = uni.getStorageSync('tihuo_data');
		for (var i = 0; i < this.info.length; i++) {
			this.allFee += Number(this.info[i].yunfei);
		}
		this.njian = this.info.length;
		this.getAddress()
		uni.$on('changeAddress', (addId) => {
			this.upDataAddress(addId);
		})
		this.get_apply_deliver_price();
		this.getRule();
		this.getGeneral();
	},
	onUnload() {
		uni.removeStorageSync('tihuo')
		uni.$off('changeAddress');
	},
	onShow() {
	  if (!uni.getStorageSync("user")) {
		    uni.redirectTo({
		    	url:'../../pages/my/login'
		    })
		}
	},
	methods: {
		getGeneral() {
			this.$http({
				url: "api/common/get_general",
			})
				.then((res) => {
					if (res.data.code == 1) {
						this.app_wxpay_status = res.data.data.app_wxpay_status; //代表app微信支付开启，
						this.app_alipay_status = res.data.data.app_alipay_status; //代表app支付宝支付开启
						this.epay_alipay_status = res.data.data.epay_alipay_status; //代表h5支付宝开启，
						this.epay_wxpay_status = res.data.data.epay_wxpay_status; //代表h5微信支付开启
					}
				})
				.catch((err) => { });
		},
		//获取是否有额外费用
		get_apply_deliver_price() {
			this.$http({
				url: "api/order/get_apply_deliver_price",
				data: {
					ids: this.ids
				}
			}).then(res => {
				console.log(res);
				if (res.data.code == 1) {
					this.yunfei = res.data.data;
				}
			}).catch(err => { });
		},
		getRule() {
			this.$http({
				url: "api/common/get_agreement",
				data: {
					name: 'delivery_rule'
				}
			}).then(res => {
				if (res.data.code == 1) {
					this.rule = res.data.data.content
				} else {
					this.$tip.tip(res.data.msg)
				}
			}).catch(err => { });
		},
		pay() {
			if (!this.address) {
				this.$tip.tip(this.$t("请选择地址"))
				return
			}
			if (this.yunfei > 0) {
				if (this.paymode == "") {
					this.$tip.tip(this.$t("请选择支付方式"))
					return
				}
			}
			this.$http({
				url: "api/order/apply_deliver",
				data: {
					ids: this.ids,
					address_id: this.address.id,
					paymode: this.paymode,
					remarks: this.remarks,
					// #ifdef H5
					client: 0,
					// #endif

					// #ifdef MP-WEIXIN
					client: 1,
					// #endif

					// #ifdef APP-PLUS
					client: 2,
					// #endif
				}
			}).then(res => {
				if (res.data.code == 1) {
					// #ifdef H5
					this.fun.go_h5pay(res.data.data);
					// #endif

					// #ifdef MP-WEIXIN
					this.fun.go_mpwxpay(res.data.data).then(payres => {
						this.$tip.tip(this.$t("支付成功"), "success")
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/mall/paySuccexx'
							})
						}, 700)
					}).catch(payerr => {
						this.$tip.tip(this.$t('支付失败'))
					});
					// #endif

					// #ifdef APP-PLUS
					if (this.paymode == 'wechat') {
						this.fun.go_appwxpay(res.data.data.pay_data).then(payres => {
							this.$tip.tip(this.$t("支付成功"), "success")
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/mall/paySuccexx'
								})
							}, 700)
						}).catch(payerr => {
							this.$tip.tip(this.$t('支付失败'))
						});
					} else {
						this.fun.go_appalipay(res.data.data.pay_data).then(payres => {
							this.$tip.tip(this.$t("支付成功"), "success")
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/mall/paySuccexx'
								})
							}, 700)
						}).catch(payerr => {
							this.$tip.tip(this.$t('支付失败'))
						});
					}
					// #endif
				} else if (res.data.code == 8) {
					this.$tip.tip(res.data.msg, "success")
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/mall/paySuccexx'
						})
					}, 700)
					return
				} else {
					this.$tip.tip(res.data.msg)
				}
			}).catch(err => { });

		},
		getAddress() {
			this.$http({
				url: "api/member/my_address",
			}).then(res => {
				console.log(res);
				if (res.data.code == 1) {
					if (res.data.data.length == 0) {
						this.isAdd = false
					} else {
						this.isAdd = true
					}
					this.address = res.data.data[0];
					// 新加的获取地址code接口
					this.getAmount(res.data.data[0]);
				} else {
					this.$tip.tip(res.data.msg)
					this.isAdd = false
				}
			}).catch(err => { });
		},
		// 更新收货地址
		upDataAddress(id) {
			this.$http({
				url: "api/member/get_address",
				data: {
					address_id: id
				}
			}).then(res => {
				console.log(res, "dizhi");
				if (res.data.code == 1) {
					this.address = res.data.data
					this.isAdd = true;
					this.getAmount(res.data.data);
				}

			}).catch(err => { });
		},
		// 新加的获取地址code接口
		getAmount(item) {
			// this.$http({
			// 	url: "api/goods/find_freight",
			// 	data: {
			// 		province_code: '',
			// 		province_name: item.province
			// 	}
			// }).then(res => {
			// 	if (res.data.code == 1) {
			// 		let fee = res.data.data;
			// 		this.amount = fee ? fee.amount : '';
			// 	} else {
			// 		this.$tip.tip(res.data.msg);
			// 	}
			// })
		},
		// 选择支付方式
		selectPay(mode) {
			this.paymode = mode
		},
	}
}
</script>

<style>
@import url("../../static/css/confirmOrder.css");
</style>
